package com.crm.service.impl;

import com.crm.context.BaseContext;
import com.crm.dto.BusinessDTO;
import com.crm.dto.GainDto;
import com.crm.mapper.BusinessMapper;
import com.crm.pojo.Business;
import com.crm.pojo.BusinessRecord;
import com.crm.pojo.Referredsubject;
import com.crm.result.PageResult;
import com.crm.service.BusinessService;
import com.crm.vo.BusinessVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;


    @Override
    public PageResult queryBusiness(BusinessDTO dto) {

        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

       List<Business> list= businessMapper.query(dto);

        PageInfo<Business> info = new PageInfo<>(list);

        PageResult pageResult = new PageResult(info.getTotal(), info.getList());
        return pageResult;


    }
    @Override
    public void addBusiness(Business business) {

        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(3);
        business.setUserId(BaseContext.getCurrentId());

        businessMapper.addBusiness(business);
    }
    @Override
    public Business getBusinessById(Long id) {
        Business business =  businessMapper.getBusnissById(id);
        return  business;
    }

    @Override
    public List<BusinessRecord> getHistory(Long id) {
        List<BusinessRecord> list = businessMapper.getHistoryById(id);
        return list;
    }

    @Override
    public PageResult queryBusinesspoll(BusinessDTO dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        List<Business> list= businessMapper.querypool(dto);

        PageInfo<Business> info = new PageInfo<>(list);

        PageResult pageResult = new PageResult(info.getTotal(), info.getList());
        return pageResult;
    }

    @Override
    public void gain(GainDto dto) {
      /*  List<Business> list=businessMapper.queryBusinessById(ids);
        for (Business business : list) {
            business.setStatus(3);
            business.setUpdateTime(LocalDateTime.now());
            String name=businessMapper.selectUserByid(business.getUserId());
            business.setCreate_by(name);
            businessMapper.gain(business);*/
        businessMapper.gain(dto.getIds());

        }


    }



